﻿using System;
using System.Data;
using System.Web.UI.WebControls;

using Wicresoft.Common;
using Wicresoft.JDPostBoard.Entity;

public partial class Gift_GiftManageList : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 礼品名称";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1 and IsEnable='True' ";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.ShowAdvancedSearch = false;

        this.Title = "礼品种类管理";
        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "CreateTime";
            this.Order.OrderDirection = "DESC";
            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    private void BindData()
    {
        int recordCount = 0;
        DataTable dtGiftList = DataAccess.Select(typeof(Gift), this.Pager.PageIndex, this.Pager.PageSize, SearchClause, this.Order.OrderColumn + " " + this.Order.OrderDirection, ref recordCount);
        Pager.RecordCount = recordCount;

        this.rptGiftList.DataSource = dtGiftList;
        rptGiftList.DataBind();

    }

    protected void rptGiftList_OnItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        string pk = e.CommandArgument.ToString();
        if (e.CommandName == "Delete")
        {
            Gift post = new Gift(new Guid(pk));
            post.Load();
            post.IsEnable = false;
            post.Save();
            WebCommon.ShowMessage(this, "删除成功");
            BindData();
        }
        if (e.CommandName == "Edit")
        {
            WebCommon.OpenWindow(this, "GiftEdit.aspx?GiftID=" + pk + "&Type=Edit&nc=" + new Random().Next(), "width=330,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,left=' + (screen.width - 330) / 2 + ',top=' + (screen.height - 300) / 2+'");
        }
    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchClause = "1=1 and IsEnable='True' ";

        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause += string.Format(" and {0} LIKE N'%{1}%'", Gift.SQLCOL_GIFTNAME, tbSearch.Text);

        BindData();
    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        BindData();

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
